package lab_2;

import com.sun.messaging.ConnectionConfiguration;
import javax.jms.Destination;
import javax.jms.Connection;
import javax.jms.Session;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.JMSException;
import javax.jms.MessageFormatException;

/**
 *
 * @author grp02 - Pascal Charreix and Meike Paetz
 *
 * This is the TravelAgency JMS client which acts as both sender and receiver.
 * Receives ObjectMessage from SmartTours synchronously and displays invoice at 
 * the console. Status will be changed to 'paid'.
 * Afterwards 'paid' invoice will be send back to SmartTours.
 */
public class TravelAgencyClient {

    private Connection connection;
    private MessageConsumer consumer;
    private MessageProducer producer;
    private Session session;

    /**
     *
     * This is the TravelAgencyClient main
     */
    public static void main(String[] args) {
        Invoice invoice;
        TravelAgencyClient TAC = new TravelAgencyClient();
        invoice = TAC.receiveInvoice();
        invoice.payment();
        TAC.sendInvoice(invoice);
        TAC.close();
    }

    /**
     *
     * TravelAgencyClient Constructor
     */
    public TravelAgencyClient() {
        setupConnection();
    }

    /**
     *
     * sets up connection
     */
    private void setupConnection() {
        com.sun.messaging.ConnectionFactory connectionFactory = null;
        Destination destRec = null;
        Destination destSend = null;
        final String host = "192.168.6.127";
        try {
            connectionFactory = new com.sun.messaging.ConnectionFactory();
            connectionFactory.setProperty(ConnectionConfiguration.imqAddressList, host + ":7676");
        } catch (JMSException e) {
            System.err.println("Error creating connection factory: " + e.toString());
            System.exit(1);
        }
        try {
            destRec = (Destination) new com.sun.messaging.Queue("grp02AgencyQueue");
            destSend = (Destination) new com.sun.messaging.Queue("grp02SmartToursQueue");
        } catch (JMSException e) {
            System.err.println("Error creating queue/topic or setting destination: " + e.toString());
            System.exit(1);
        }
        try {
            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            consumer = session.createConsumer(destRec);
            producer = session.createProducer(destSend);
        } catch (JMSException e) {
            System.err.println("Error creating connection, session or consumer: " + e.toString());
        }
    }

    /**
     *
     * closes connection
     */
    private void close() {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                System.err.println("JMS exception occurred: " + e.toString());
            }
        }
    }

    /**
     *
     * receives invoice from SmartTours
     */
    private Invoice receiveInvoice() {
        Invoice invoice = null;
        try {
            connection.start();
            Message m = consumer.receive();
            if (m != null && m instanceof ObjectMessage) {
                ObjectMessage objectMessage = (ObjectMessage) m;
                invoice = (Invoice) objectMessage.getObject();
                System.out.println("Reading object message: " + invoice.getInvoiceInformation());
            }
        } catch (MessageFormatException e) {
            System.err.println("Object deserialization failed: " + e.toString());
        } catch (JMSException e) {
            System.err.println("JMS exception occurred: " + e.toString());
        }
        return invoice;
    }

    /**
     *
     * sends invoice to SmartTours
     */
    public void sendInvoice(Invoice invoice) {
        try {
            ObjectMessage objectMessage = session.createObjectMessage();
            objectMessage.setObject(invoice);
            System.out.println("Sending object message: " + ((Invoice) objectMessage.getObject()).getInvoiceInformation());
            producer.send(objectMessage);
        } catch (MessageFormatException e) {
            System.err.println("Object deserialization failed: " + e.toString());
        } catch (JMSException e) {
            System.err.println("JMS exception occurred: " + e.toString());
        }
    }
}
